package com.ruoyi.web.controller.pollution;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pollution.domain.BusPsCodPollutant;
import com.ruoyi.pollution.service.IBusPsCodPollutantService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pollution.domain.BusPsCodOutputpollutant;
import com.ruoyi.pollution.service.IBusPsCodOutputpollutantService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 排放口污染物设置Controller
 * 
 * @author maliang
 * @date 2025-09-18
 */
@RestController
@RequestMapping("/pollution/outputpollutant")
public class BusPsCodOutputpollutantController extends BaseController
{
    @Autowired
    private IBusPsCodOutputpollutantService busPsCodOutputpollutantService;
    @Autowired
    private IBusPsCodPollutantService busPsCodPollutantService;

    /**
     * 查询排放口污染物设置列表
     */
    //@PreAuthorize("@ss.hasPermi('pollution:outputpollutant:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusPsCodOutputpollutant busPsCodOutputpollutant)
    {
        startPage();
        List<BusPsCodOutputpollutant> list = busPsCodOutputpollutantService.selectBusPsCodOutputpollutantList(busPsCodOutputpollutant);
        return getDataTable(list);
    }

    @GetMapping("/listall")
    public AjaxResult listall(BusPsCodOutputpollutant busPsCodOutputpollutant)
    {
        List<BusPsCodOutputpollutant> list = busPsCodOutputpollutantService.selectBusPsCodOutputpollutantList(busPsCodOutputpollutant);
        return AjaxResult.success(list);
    }

    @GetMapping("/listbyoutput/{outportcode}")
    public AjaxResult listbyoutput(@PathVariable("outportcode") String outportcode)
    {
        BusPsCodOutputpollutant busPsCodOutputpollutantParam = new BusPsCodOutputpollutant();
        busPsCodOutputpollutantParam.setOutportcode(outportcode);
        List<BusPsCodOutputpollutant> busPsCodOutputpollutantList = busPsCodOutputpollutantService.selectBusPsCodOutputpollutantList(busPsCodOutputpollutantParam);
        List<Map> mapList = new ArrayList<>();
        List<BusPsCodPollutant> busPsCodPollutantList = busPsCodPollutantService.selectBusPsCodPollutantList(new BusPsCodPollutant());
        if(!CollectionUtils.isEmpty(busPsCodPollutantList)){
            for (BusPsCodPollutant busPsCodPollutant : busPsCodPollutantList) {
                Map map = new HashMap();
                map.put("pollutantcode",busPsCodPollutant.getPollutantcode());
                map.put("pollutantname",busPsCodPollutant.getPollutantname());
                map.put("status","0");
                if(!CollectionUtils.isEmpty(busPsCodOutputpollutantList)){
                    for (BusPsCodOutputpollutant codOutputpollutant : busPsCodOutputpollutantList) {
                        if(codOutputpollutant.getPollutantcode().equals(busPsCodPollutant.getPollutantcode())){
                            map.put("status","1");
                            break;
                        }
                    }
                }
                mapList.add(map);
            }
        }
        return AjaxResult.success(mapList);
    }
    /**
     * 导出排放口污染物设置列表
     */
    //@PreAuthorize("@ss.hasPermi('pollution:outputpollutant:export')")
    @Log(title = "排放口污染物设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusPsCodOutputpollutant busPsCodOutputpollutant)
    {
        List<BusPsCodOutputpollutant> list = busPsCodOutputpollutantService.selectBusPsCodOutputpollutantList(busPsCodOutputpollutant);
        ExcelUtil<BusPsCodOutputpollutant> util = new ExcelUtil<BusPsCodOutputpollutant>(BusPsCodOutputpollutant.class);
        util.exportExcel(response, list, "排放口污染物设置数据");
    }

    /**
     * 获取排放口污染物设置详细信息
     */
    //@PreAuthorize("@ss.hasPermi('pollution:outputpollutant:query')")
    @GetMapping(value = "/{pollutantcode}")
    public AjaxResult getInfo(@PathVariable("pollutantcode") String pollutantcode)
    {
        return success(busPsCodOutputpollutantService.selectBusPsCodOutputpollutantByPollutantcode(pollutantcode));
    }

    /**
     * 新增排放口污染物设置
     */
    //@PreAuthorize("@ss.hasPermi('pollution:outputpollutant:add')")
    @Log(title = "排放口污染物设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusPsCodOutputpollutant busPsCodOutputpollutant)
    {
        return toAjax(busPsCodOutputpollutantService.insertBusPsCodOutputpollutant(busPsCodOutputpollutant));
    }

    /**
     * 修改排放口污染物设置
     */
    //@PreAuthorize("@ss.hasPermi('pollution:outputpollutant:edit')")
    @Log(title = "排放口污染物设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BusPsCodOutputpollutant busPsCodOutputpollutant)
    {
        return toAjax(busPsCodOutputpollutantService.updateBusPsCodOutputpollutant(busPsCodOutputpollutant));
    }

    /**
     * 删除排放口污染物设置
     */
    //@PreAuthorize("@ss.hasPermi('pollution:outputpollutant:remove')")
    @Log(title = "排放口污染物设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{pollutantcodes}")
    public AjaxResult remove(@PathVariable String[] pollutantcodes)
    {
        return toAjax(busPsCodOutputpollutantService.deleteBusPsCodOutputpollutantByPollutantcodes(pollutantcodes));
    }
}
